package com.demo.core.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.util.CollectionUtils;

import com.demo.core.cache.CacheKey;
import com.demo.core.cache.CacheWrapper;
import com.demo.core.dao.FunctionDao;
import com.demo.core.dao.impl.base.PageCoreDaoSupport;
import com.demo.core.model.Function;
import com.demo.core.model.support.GeneralExecuteModel;
import com.demo.core.model.support.converter.AdjacencyListToChainedObjectConverter;

/**
 * 功能数据库访问层实现类
 * 
 * @author ryuu.kk
 */
public class FunctionDaoImpl extends PageCoreDaoSupport<Function, Long> implements FunctionDao {
	/**
	 * 功能空间 
	 */
	protected final static String FUNCTION_SPACE = "functionSpace.";
	/**
	 * 模型表名
	 */
	protected final static String TABLE_NAME = "T_CORE_FUNCTION";
	/**
	 * 模型表主键ID
	 */
	protected final static String TABLE_PK_NAME = "ID";
	
	@Override
	public int updateNestedSet(Map<String, Object> functionInfo) {
		return getSqlMapClientTemplate().update(FUNCTION_SPACE + "updateNestedSet", functionInfo);
	}
	
	@Override
	public int delete(Long id) {
		Map<String, Object> params = new HashMap<String, Object>();
		int cnt = 0;
		params.put("id", id);
		delete(params);
		if (params.containsKey("cnt")) {
			cnt = Integer.parseInt(params.get("cnt").toString());
		}
		return cnt;
	}
	
	@Override
	public Function tree(Map<String, Object> params) {
		List<Function> result = query(params);
		return AdjacencyListToChainedObjectConverter.conver(result);
	}
	
	@Override
	public Function tree(Long functionId, int... depths) {
		return tree(functionId, null, depths);
	}
	
	@Override
	public Function tree(Long functionId, Long siteGroupId, int... depths) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", functionId);
		params.put("siteGroupId", siteGroupId);
		params.put("depthList", CollectionUtils.arrayToList(depths));
		return tree(params);
	}
	@Override
	public List<Function> query(Long functionId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", functionId);
		return query(params);
	}
	@Override
	public List<Function> query(Long functionId, Long siteGroupId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", functionId);
		params.put("siteGroupId", siteGroupId);
		return query(params);
	}
	
	@SuppressWarnings("unchecked")
	private List<Function> query(Map<String, Object> params) {
		CacheKey key = null;
		List<Function> result = null;
		CacheWrapper<CacheKey, Object> cache = null;
		
		if (isUseCache() && (cache = getCache()) != null) {
			key = new CacheKey();
			key.update("tree");
			key.update(params.hashCode());
			result = (List<Function>) cache.get(key);
		}
		if (result == null) {
			result  = getSqlMapClientTemplate().queryForList(FUNCTION_SPACE + "queryTree", params);
			if (isUseCache() && cache != null && result != null) {
				cache.put(key, result);
			}
		}
		return result;
	}
	
	@Override
	protected String nameSpace() {
		return FUNCTION_SPACE;
	}
	@Override
	protected void doInit(GeneralExecuteModel<Long> model) {
		model.setPk(TABLE_PK_NAME);
		model.setTable(TABLE_NAME);
	}
}
